#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2018/11/20 18:09
# @Author  : yangdy
# @Email   : yangdy@egu360.com
# @File    : 中级-分数到小数.py
# @Software: PyCharm

from utils.main import main, opt_list

class Solution:
    def fractionToDecimal(self, numerator, denominator):
        """
        :type numerator: int
        :type denominator: int
        :rtype: str
        """
        m = {}
        n = numerator
        d = denominator

        def sig(num):
            return 1 if num > 0 else (-1 if num < 0 else 0)
        sn = sig(n)
        sd = sig(d)
        sa = sn * sd
        n *= sn
        d *= sd
        p = n // d
        yu = n % d
        pp = []
        m[yu] = {'i': -1}
        ans = '%d' % p
        lp = 0
        while yu:
            pp.append('%d' % (yu*10//d))
            yy = yu*10 % d
            yu = yy
            if m.get(yy):
                break
            m[yu] = {'i': lp}
            lp += 1
        e = m.get(yu)
        print(yu, m, pp)
        print(e)
        if e:
            if not yu:
                ans += ('.' + ''.join(pp)) if pp else ''
            else:
                # i = 0 if e['i'] == -1 else e['i']
                i = e['i']
                ans += '.' + ''.join(pp[:i+1]) + '(' + ''.join(pp[i+1:]) + ')'
        return ('' if sa >= 0 else '-') + ans


def opt(numerator, denominator):
    solve = Solution()
    print('%d / %d' % (numerator, denominator))
    print(solve.fractionToDecimal(numerator, denominator))


def opt1():
    opt(1, 2)


opt_list.append(opt1)


def opt2():
    opt(4, 333)


opt_list.append(opt2)


def opt3():
    opt(1, 6)


opt_list.append(opt3)

if __name__ == '__main__':
    main()
